//
//  ViewUtil.swift
//  TYYSDK
//
//  Created by SunXianHua on 2018/8/10.
//  Copyright © 2018年 TYY. All rights reserved.
//

import UIKit

extension UIView {
    
    
    static let RadiusDefault:CGFloat = 5.0;
    
    static func setRadius( _ view:UIView, radius:CGFloat = RadiusDefault)
    {
//        view.layoutIfNeeded()
//        view.layer.cornerRadius = radius;
//        view.layer.masksToBounds = true;
        
        let corner =
            UInt8(UIRectCorner.topRight.rawValue) |
            UInt8(UIRectCorner.topLeft.rawValue) |
            UInt8(UIRectCorner.bottomRight.rawValue) |
            UInt8(UIRectCorner.bottomLeft.rawValue)

        setRadius(view, radius: radius, corner: corner)
        
    }
    
    /// 可以单独设置圆角
    ///
    /// - Parameters:
    ///   - view:
    ///   - radius:
    ///   - corner: UInt8(UIRectCorner.topLeft.rawValue) | UInt8(UIRectCorner.topRight.rawValue) | UInt8(UIRectCorner.bottomLeft.rawValue) | UInt8(UIRectCorner.bottomRight.rawValue)// 这只圆角位置
    static func setRadius( _ view:UIView, radius:CGFloat = RadiusDefault, corner:UInt8)
    {
        
        view.layoutIfNeeded()
        view.layer.masksToBounds = true;
        
        let rect = view.bounds
        
        let radio = CGSize(width: radius, height: radius) // 圆角尺寸
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: UIRectCorner(rawValue: UIRectCorner.RawValue(corner)), cornerRadii: radio)
        
        let masklayer = CAShapeLayer() // 创建shapelayer
        
        masklayer.frame = view.bounds
        
        masklayer.path = path.cgPath // 设置路径
        
        view.layer.mask = masklayer
    }
    

    

}
